package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.entity.ClueRuleEntity;
import com.xbongbong.saas.domain.entity.ClueUserEntity;
import com.xbongbong.saas.domain.entity.RuleClueEntity;
import com.xbongbong.saas.domain.entity.ext.ClueEntityExt;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.model.ClueUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/12/2 15:21
 * @since v1.0
 */
@Component
public class CheckClueRulePrePrincipal implements ICheckClueRule{

    @Resource
    private ClueUserModel clueUserModel;
    @Resource
    private ClueModel clueModel;

    @Override
    public boolean canGainClue(ClueRuleEntity rule, String corpid,
                               UserEntity userEntity, Long clueId, int dataIdSize, JSONObject data) throws XbbException {
        //没有规则，不限制线索获取
        if(rule == null || Objects.equals(rule.getStatus(), 0)){
            return true;
        }

        //判断是否公海线索
        ClueEntityExt clue = clueModel.getByKey(clueId, corpid);

        Integer isPublic = clue.getData().getInteger(ClueEnum.IS_PUBLIC.getAttr());

        if(Objects.equals(isPublic, 0)){
            //非公海线索不限制
            return true;
        }

        //判断是否前负责人
        List<ClueUserEntity> clueUserList = getClueUserList(corpid, userEntity.getUserId(), clueId);
        //为空不是前负责人
        if(clueUserList==null || clueUserList.isEmpty()){
            return true;
        }

        /**
         * rule.getRuleType() 必然是 ClueRuleEnum.PRE_PRINCIPAL_RULE.getCode()
         */
        IOperateClueRuleValue operateRuleValue = OperateClueRuleValueFactory.operateRuleValueFactory(rule.getRuleType());
        JSONArray ruleValueArray = (JSONArray) operateRuleValue.toObject(rule);

        Integer minValue = CheckRuleUtil.getMinValue(ruleValueArray, userEntity);

        ClueUserEntity clueUser = clueUserList.get(0);
        // 退回时间
        Long backTime = clueUser.getBackTime();
        Long now = DateTimeUtil.getInt();
        Integer diffDay= DateTimeUtil.betweenDays(backTime,now);
        if(diffDay > minValue){
            return true;
        }

        return false;
    }

    public List<ClueUserEntity> getClueUserList(String corpid, String userId, Long clueId){
        Map<String, Object> param = new HashMap<>(6);
        param.put("dataId", clueId);
        param.put("isMain", 1);
        param.put("corpid", corpid);
        param.put("userId", userId);
        param.put("orderByStr", "back_time desc");
        param.put("del", -1);
        List<ClueUserEntity> clueUserList =clueUserModel.findEntitys(param);

        return clueUserList;
    }

    @Override
    public List<RuleClueEntity> getBackClueList(UserEntity userEntity,
                                                        ClueRuleEntity rule, List<RuleClueEntity> clueList,
                                                        Integer advanceTime) throws XbbException {
        return new ArrayList<>();
    }

    @Override
    public List<RuleClueEntity> getBackClueList(UserEntity userEntity,
                                                        ClueRuleEntity rule, List<RuleClueEntity> clueList)
            throws XbbException {
        return new ArrayList<>();
    }
}
